package org.liuyuantao.lucene.tika;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.liuyuantao.lucene.utils.IKAnalyzer5x;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;

/**
 * 使用tika解析非文本文件
 */
public class TikaExample {

    private String filePath;

    private String indexPath;

    public TikaExample() {
    }

    public TikaExample(String filePath, String indexPath) {
        this.filePath = filePath;
        this.indexPath = indexPath;
    }

    public void index() {
        try {
            Directory dir = FSDirectory.open(Paths.get(indexPath));
            IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(new IKAnalyzer5x(true)));
            writer.deleteAll();
            Document doc = new Document();
            doc.add(new TextField("content", new Tika().parse(Paths.get(filePath))));
            writer.addDocument(doc);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * tika解析非文本比较简单的方式,但是效率没下面的高
     *
     * @param f
     * @return
     * @throws IOException
     * @throws TikaException
     */
    public String tikaTool(File f) throws IOException, TikaException {
        Tika tika = new Tika();
        Metadata metadata = new Metadata();
        metadata.set(Metadata.RESOURCE_NAME_KEY, f.getName());
        String str = tika.parseToString(new FileInputStream(f), metadata);
        for (String name : metadata.names()) {
            System.out.println(name + ":" + metadata.get(name));
        }
        return str;
    }

    /**
     * tika解析非文本相对比较复杂
     *
     * @param filePath
     * @return
     */
    public String fileToTxt(String filePath) {
        Parser parser = new AutoDetectParser();
        InputStream in = null;
        try {
            Path path = Paths.get(filePath);
            File file = path.toFile();
            Metadata metadata = new Metadata();
            metadata.set(Metadata.RESOURCE_NAME_KEY, file.getName());
            in = new FileInputStream(file);
            ContentHandler handler = new BodyContentHandler();
            ParseContext context = new ParseContext();
            context.set(Parser.class, parser);
            parser.parse(in, handler, metadata, context);
            for (String name : metadata.names()) {
                System.out.println(name + ":" + metadata.get(name));
            }
            return handler.toString();
        } catch (IOException | SAXException | TikaException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
